<template>
  <div>
    <!-- 卡片视图区 -->
    <el-card>
      <el-row :gutter="25">
        <el-col :span="10">
          <!-- 搜索添加 -->
          <el-input placeholder="请输入搜索内容" v-model="queryInfo.query" clearable @clear="getOrderList()">
            <el-button slot="append" icon="el-icon-search" @click="getOrderList()"></el-button>
          </el-input>
        </el-col>
      </el-row>
      <!-- 订单列表 -->
      <el-table :data="orderList" border stripe>
        <el-table-column type="index"></el-table-column>
        <el-table-column label="订单编号" prop="ord_code"></el-table-column>
        <el-table-column label="产品名称" prop="ord_pro_name" width="100"></el-table-column>
        <el-table-column label="产品数量" prop="ord_pro_num"></el-table-column>
        <el-table-column label="投标截止" prop="ord_ddl" width="100"></el-table-column>
        <el-table-column label="交货时间" prop="ord_del_date" width="100"></el-table-column>
        <el-table-column label="收货人" prop="ord_rece"></el-table-column>
        <el-table-column label="联系电话" prop="ord_rece_tel"></el-table-column>
        <el-table-column label="收货地址" prop="ord_loc"></el-table-column>
        <el-table-column label="订单状态" prop="ord_state"></el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="queryInfo.pageNum"
          :page-sizes="[1, 2, 5, 100]"
          :page-size="queryInfo.pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
      ></el-pagination>
    </el-card>

    <!-- 创建订单对话框 -->
    <el-dialog title="添加新订单" :visible.sync="addDialogVisible" width="50%"
               @close="addDialogClosed">
      <!-- 内容主体区域 -->
      <el-form :model="addForm" :rules="addFormRules"
               ref="addFormRef" label-width="70px">

        <!-- 产品名称 -->
        <el-form-item label="产品名称" prop="ord_pro_name">
          <el-select v-model="addForm.ord_pro_name">
            <el-option :value="product.pro_name" v-for="product in productList">{{product.pro_name}}</el-option>
          </el-select>
        </el-form-item>

        <!-- 产品数目 -->
        <el-form-item label="产品数目" prop="ord_pro_num">
          <el-input v-model="addForm.ord_pro_num"></el-input>
        </el-form-item>

        <!-- 截止日期 -->
        <el-form-item label="投标截至" prop="ord_ddl">
          <el-input v-model="addForm.ord_ddl" type="date"></el-input>
        </el-form-item>

        <!-- 收货日期 -->
        <el-form-item label="收货日期" prop="ord_del_date">
          <el-input v-model="addForm.ord_del_date" type="date"></el-input>
        </el-form-item>

        <!-- 联系人 -->
        <el-form-item label="联系人" prop="ord_rece">
          <el-input v-model="addForm.ord_rece"></el-input>
        </el-form-item>

        <!-- 联系电话 -->
        <el-form-item label="联系电话" prop="ord_rece_tel">
          <el-input v-model="addForm.ord_rece_tel"></el-input>
        </el-form-item>

        <!-- 收货地址 -->
        <el-form-item label="收货地址" prop="ord_loc">
          <el-input v-model="addForm.ord_loc"></el-input>
        </el-form-item>
      </el-form>
      <!-- 内容底部区域 -->
      <span slot="footer" class="dialog-footer">
        <el-button @click="addDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addOrder">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 修改用户对话框 -->
    <el-dialog title="修改用户信息" :visible.sync="editDialogVisible" width="50%" @colse="editDialogClosed">
      <el-form :model="editForm" :rules="editFormRules" ref="editFormRef" label-width="70px">
        <el-form-item prop="ad_id" hidden>
          <el-input v-model="editForm.ord_id" hidden></el-input>
        </el-form-item>

        <!-- 产品名称 -->
        <el-form-item label="产品名称" prop="ord_pro_name">
          <el-select v-model="editForm.ord_pro_name">
            <el-option :value="product.pro_name" v-for="product in productList">{{product.pro_name}}</el-option>
          </el-select>
        </el-form-item>

        <!-- 产品数目 -->
        <el-form-item label="产品数目" prop="ord_pro_num">
          <el-input v-model="editForm.ord_pro_num"></el-input>
        </el-form-item>

        <!-- 截止日期 -->
        <el-form-item label="投标截至" prop="ord_ddl">
          <el-input v-model="editForm.ord_ddl" type="date"></el-input>
        </el-form-item>

        <!-- 收货日期 -->
        <el-form-item label="收货日期" prop="ord_del_date">
          <el-input v-model="editForm.ord_del_date" type="date"></el-input>
        </el-form-item>

        <!-- 联系人 -->
        <el-form-item label="联系人" prop="ord_rece">
          <el-input v-model="editForm.ord_rece"></el-input>
        </el-form-item>

        <!-- 联系电话 -->
        <el-form-item label="联系电话" prop="ord_rece_tel">
          <el-input v-model="editForm.ord_rece_tel"></el-input>
        </el-form-item>

        <!-- 收货地址 -->
        <el-form-item label="收货地址" prop="ord_loc">
          <el-input v-model="editForm.ord_loc"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="editOrderInfo">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
export default {
  data() {
    return {
      // 请求数据
      queryInfo: {
        query: "",
        pageNum: 1,
        pageSize: 5
      },
      orderList: [],// 用户列表
      productList:[],//可选产品列表
      total: 0, // 最大数据记录
      addDialogVisible: false ,// 对话框显示
      // 添加用户表单项
      addForm: {
        ord_id:'',
        ord_code:'',
        ord_pro_name:'',
        ord_pro_num:'',
        ord_del_date:'',
        ord_ddl:'',
        ord_rece:'',
        ord_rece_tel:'',
        ord_loc:'',
        ord_state:''
      },
      // 控制修改用户对话框显示/隐藏
      editDialogVisible:false,
      // 修改用户信息
      editForm:{},
      // 验证规则
      addFormRules:{

      },
      // 修改用户表单验证规则
      editFormRules:{

      },
    };
  },
  created() {
    this.getOrderList();
    this.getProductList();
  },
  methods: {
    async getOrderList() {
      // 调用post请求
      const { data: res } = await this.$http.get("order/orderList", {
        params: this.queryInfo
      });
      for(var i in res.data){
        if(res.data[i].ord_state === 0){
          res.data[i].ord_state = '未发布';
        }
        if(res.data[i].ord_state === 1){
          res.data[i].ord_state = '已发布';
        }
        if(res.data[i].ord_state === 2){
          res.data[i].ord_state = '已中标';
        }
        if(res.data[i].ord_state === 3){
          res.data[i].ord_state = '已排产';
        }
        if(res.data[i].ord_state === 4){
          res.data[i].ord_state = '已生产';
        }
        if(res.data[i].ord_state === 5){
          res.data[i].ord_state = '已发货';
        }
        if(res.data[i].ord_state === 6){
          res.data[i].ord_state = '已结束';
        }
      }
      this.orderList= res.data; // 将返回数据赋值

      //使用for循环更新对应的变量
      this.total = res.numbers; // 总个数
    },
    // 监听pageSize改变的事件
    handleSizeChange(newSize) {
      this.queryInfo.pageSize = newSize;
      this.getOrderList(); // 数据发生改变重新申请数据
    },
    // 监听pageNum改变的事件
    handleCurrentChange(newPage) {
      this.queryInfo.pageNum = newPage;
      this.getOrderList(); // 数据发生改变重新申请数据
    },
    // 监听添加用户
    addDialogClosed(){
      this.$refs.addFormRef.resetFields();// 重置表单项
    },
    // 添加用户
    addOrder(){
      this.$refs.addFormRef.validate(async valid =>{
        console.log(valid);
        if( !valid ) return;
        // 发起请求
        const {data:res} = await this.$http.post("order/addOrder",this.addForm);
        if (res !== "success") {
          userinfo.state = !userinfo.state;
          return this.$message.error("操作失败！！！");
        }
        this.$message.success("操作成功！！！");
        //隐藏
        this.addDialogVisible = false;
        this.getOrderList();
      })
    },
    async getProductList(){
      const { data: res } = await this.$http.post("product/allProductList");
      this.productList = res.data;
      console.log(this.productList);
    },
    // 展示修改框
    async showEditDialog(id){

      const {data:res} = await this.$http.post("order/getUpdate?id="+id);
      if(res.flag === 'ok'){
        this.editForm = res.data;
        this.editDialogVisible = true;
      }else{
        this.$message.error("订单已发布，无法修改");
      }
    },
    // 关闭窗口
    editDialogClosed(){
      this.$refs.editFormRef.resetFields();
    },
    // 确认修改
    editOrderInfo(){
      this.$refs.editFormRef.validate(async valid =>{
        console.log(valid);
        if( !valid ) return;
        // 发起请求
        const {data:res} = await this.$http.post("order/editOrder",this.editForm);
        console.log(res);
        if (res !== "success") return this.$message.error("操作失败！！！");
        this.$message.success("操作成功！！！");
        //隐藏
        this.editDialogVisible = false;
        this.getOrderList();
      });
    },
    // 删除按钮
    async deleteOrder(id){
      // 弹框
      const confirmResult = await this.$confirm('此操作将永久删除该订单, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(err => err)
      // 成功删除为confirm 取消为 cancel
      if(confirmResult!=='confirm'){
        return this.$message.info("已取消删除");
      }
      const {data:res} = await this.$http.post("order/deleteOrder?id="+id);
      if (res !== "success") {
        return this.$message.error("订单已发布，无法删除");
      }
      this.$message.success("操作成功！！！");
      this.getOrderList();
    },
    //发布订单
    async releaseOrder(id){
      // 弹框
      const confirmResult = await this.$confirm('此操作将发布该订单, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(err => err)
      // 成功删除为confirm 取消为 cancel
      if(confirmResult!=='confirm'){
        return this.$message.info("已取消发布");
      }
      const {data:res} = await this.$http.post("order/releaseOrder?id="+id);
      if (res !== "success") {
        return this.$message.error("订单已发布，无法继续发布");
      }
      this.$message.success("操作成功！！！");
      this.getOrderList();
    },
    async selectOrder(id){
      // 弹框
      const confirmResult = await this.$confirm('此操作将选标, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(err => err)
      // 成功删除为confirm 取消为 cancel
      if(confirmResult!=='confirm'){
        return this.$message.info("已取消选标");
      }
      const {data:res} = await this.$http.post("order/isSelected?id="+id);
      if (res !== "success") {
        return this.$message.error("订单已中标，无法继续选标");
      }
      //将该订单信息存入session中，方便在后续页面中调用
      window.sessionStorage.setItem("ord_id",id);
      this.$router.push("/bideList");
    },
    async receiveOrder(id){
      // 弹框
      const confirmResult = await this.$confirm('此操作将收货, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(err => err)
      // 成功删除为confirm 取消为 cancel
      if(confirmResult!=='confirm'){
        return this.$message.info("已取消发收货");
      }
      const {data:res} = await this.$http.post("order/receiveOrder?id="+id);
      if (res !== "success") {
        return this.$message.error("操作失败");
      }
      this.$message.success("操作成功！！！");
      this.getOrderList();
    }
  }
};
</script>
<style lang="less" scoped>
</style>